package com.kandinfo.base.admin.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;

import com.kandinfo.base.admin.domain.WorkGroupData;
import com.kandinfo.base.common.repository.BaseRepository;


/**
 * 工作组
 * @author wenchongyang
 *
 */
public interface WorkGroupDataRepository extends BaseRepository<WorkGroupData, Long> {

    @Query(value = "select * from workgroup_data where group_id = ?1 and ref_type =?2", nativeQuery = true)
    List<WorkGroupData> findByGroupId(Long id , Integer type);

    /**
     * 根据题库ID获取小组
     * 
     * @param WorkGroupId
     * @return
     */
    @Query(value = "SELECT * FROM workgroup_data WHERE ref_id=?1 ", nativeQuery = true)
    List<WorkGroupData> getByQuestionBankId(Long questionBankId);
    
    /**
     * 
     * Description: 根据关联id查询用户组关联信息
     *
     * @param refId 
     * @param type 数据权限类型
     * @return
     */
    @Query(value = "select * from workgroup_data where ref_id = ?1 and ref_type =?2", nativeQuery = true)
    List<WorkGroupData> findByRefId(Long refId , Integer type);
    
    @Query(value = "select * from workgroup_data where group_id in (select ug.group_id from user_group ug where ug.user_id=?1)"
            + " and ref_type =?2", nativeQuery = true)
    List<WorkGroupData> findPermissionByUserId(Long userId , Integer type);
}
